package com.neymar.controller;


import com.neymar.entity.User;
import com.neymar.logEnum.ObjectionTypeEnum;
import com.neymar.service.LogService;
import com.neymar.service.UserService;
import com.neymar.vo.R;
import com.neymar.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author neymar
 * @since 2021-06-02
 */
@RestController
@RequestMapping("/user")
@Api(tags = {"用户登录信息操作接口"})
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    @ApiOperation("保存用户信息")
    @PostMapping("/save")
    public R save(@RequestBody User user) {
        int flag = userService.save(user);
        if (flag != 0) {
            logService.save(user.getUserId(), 1, 4);
            return R.ok();
        }
        return R.fail();
    }

    @ApiOperation("根据ID查找用户信息[非登录验证]")
    @GetMapping("/findByUserId/{userId}")
    public User findByUserId(@PathVariable("userId") String userId) {
        return userService.findOneById(userId);
    }

    @ApiOperation("根据ID查找用户信息[登录验证]")
    @GetMapping("/find/{userId}/{userPassword}")
    public R findUser(@PathVariable("userId") String userId,@PathVariable("userPassword") String userPassword) {
        User userw = new User();
        userw.setUserId(userId);
        userw.setUserPassword(userPassword);
        return R.ok(userService.findUser(userw));
    }

    @ApiOperation("根据ID删除用户信息")
    @DeleteMapping("/delete/{userId}")
    public R deleteUserById(@PathVariable("userId") String userId) {
        int flag = userService.deleteUserById(userId);
        if (flag != 0) {
            logService.save(userId, 1, 4);
            return R.ok();
        }
        return R.fail();
    }

    @ApiOperation("根据ID修改用户信息")
    @PutMapping("/update")
    public R updateUserById(@RequestBody User user) {
        int flag = userService.updateUserById(user);
        if (flag != 0) {
            logService.save(user.getUserId(), 1, 4);
            return R.ok();
        }
        return R.fail();
    }

    @ApiOperation("查看用户数量")
    @GetMapping("/showNumInfo/{operatorId}")
    public R userNumber(@PathVariable("operatorId") String operatorId) {
        logService.save(operatorId, 4, 4);
        return R.ok(userService.userNumber());
    }

    @ApiOperation("[分页查询] 用户信息")
    @GetMapping("/findUsers/{current}/{size}/{operatorId}")
    public R findAllUser(@PathVariable("current") int current,
                         @PathVariable("size") int size,
                         @PathVariable("operatorId") String operatorId) {
        logService.save(operatorId, 4, 4);
        return R.ok(userService.findAllUser(current, size));
    }

    @ApiOperation("[分页查询] 用户信息 [包含详细信息]")
    @GetMapping("/findUsersDetails/{current}/{size}/{operatorId}")
    public R findAllUserDetails(@PathVariable("current") int current,
                                @PathVariable("size") int size,
                                @PathVariable("operatorId") String operatorId) {
        logService.save(operatorId, 4, 4);
        return R.ok(userService.findAllUserDetails(current, size));
    }
}

